<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    @ok="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading">

<!--        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"-->

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="图片不存在"
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
<!--          <a @click="handleCheck(record)">打开</a>-->
          <a @click="handleYuLan(record)">预览</a>
          <a-divider type="vertical"/>
            <a@click="DownLoadClick(record)" >恢复</a>
          </span>
      </a-table>
    </a-spin>
    <doc-modal-check ref="modalForm"/>
    <EditorOpenModal ref="editOpenForm"></EditorOpenModal>
  </a-modal>
</template>

<script>
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import { httpAction ,postAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import {queryDocHistoryById} from '@/api/api'
  import {getAction} from '@/api/manage'
  import DocModalCheck from './DocModalCheck'
  import EditorOpenModal from "./EditorOpenModal";

  export default {
    name: "RecordModal",
    components: {
      JDate,
      JDictSelectTag,
      JEditor,
      JCategorySelect,
      DocModalCheck,
      EditorOpenModal
    },
    data () {
      return {
        isOpen:"",
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        selectedKeys: [],
        confirmLoading: false,
        dataSource: [],
        loading: false,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '文件名称',
            align: "center",
            dataIndex: 'docName'
          },
          {
            title: '操作人',
            align: "center",
            dataIndex: 'operator'
          },
          {
            title: '操作',
            align: "center",
            dataIndex: 'operation'
          },
          {
            title: '操作时间',
            align: "center",
            dataIndex: 'operationTime'
          },
          {
            title: '历史内容',
            dataIndex: 'action',
            align: "center",
            scopedSlots: {customRender: 'action'}
          }
        ],
        url: {
          list: "/doc/docHistory/list",
          queryById: "/doc/docHistory/queryById",
          add: "/doc/doc/add",
          edit: "/doc/doc/edit",
          recover: "/doc/doc/recover",
          queryHistoryById: "/doc/docHistoryCopy/queryById",
          YuLan: "/doc/doc/hisPdfFile",
        }
      }
    },
    created () {
    },
    methods: {
      DownLoadClick(record) {
        const that=this
        getAction(this.url.queryHistoryById,{id:record.id}).then((res) => {
          if (res.success) {
            postAction(this.url.recover,res.result).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            })
            this.loading = false
          }
        })},
      handleYuLan(record){
        var param={
          docName:record.docName,
          id:record.id
        }
        getAction(this.url.YuLan, param).then((res) => {
        })
        window.open(window._CONFIG['domianURL']+"/doc/doc/hisPdfFile?docName="+record.docName+"&id="+record.id);
      },
      handleCheck:function(record){
        this.$refs.editOpenForm.editOpen(record.id)
        this.$refs.editOpenForm.title = '查看'
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        //this.dataSource = this.model
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'docCategory','docName','docNo','dzDepartment','docSize','jic','sxTime','dqTime','isJic','download','print','audit','editorString','docStatus','docExpired'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      //@ok="handleOk"
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'docCategory','docName','docNo','dzDepartment','docSize','jic','sxTime','dqTime','isJic','download','print','audit','editorString','docStatus','docExpired'))
      },

      open(id) {
        var formData = {
          id: id,
        }
        this.dataSource=[]
        getAction(this.url.list, formData, 'get').then((res) => {
          if (res.success) {
            for (let i = 0; i < res.result.records.length; i++) {
              let temp = res.result.records[i]
              this.dataSource.push(temp)
              //console.log("temp->"+JSON.stringify(this.dataSource))
            }
            this.loading = false
          }
        })

      },
      clearForm() {
        this.form.resetFields();
        this.treeData = [];
      },
    }
  }
</script>